package com.woniuxy.java106crm.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.woniuxy.java106crm.entity.ServiceInvoice;
import com.woniuxy.java106crm.mapper.ServiceInvoiceMapper;
import com.woniuxy.java106crm.request.ResPage;
import com.woniuxy.java106crm.service.ServiceInvoiceService;
import com.woniuxy.java106crm.util.Status;
import com.woniuxy.java106crm.vo.SearchServiceVo;
import com.woniuxy.java106crm.vo.ServiceInvoiceVo;
import com.woniuxy.java106crm.vo.ServiceTypeCountVo;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.List;

/**
 * 服务单据业务层实现类
 */
@Service
public class ServiceInvoiceServiceImpl implements ServiceInvoiceService {
    @Resource
    private ServiceInvoiceMapper serviceInvoiceMapper;

    /**
     * 创建服务单据
     * @param serviceInvoice
     */
    @Override
    public void addServiceInvoice(ServiceInvoice serviceInvoice) {
        serviceInvoiceMapper.addServiceInvoice(serviceInvoice);
    }

    /**
     * 服务分配界面多条件查询所有单据
     * @param searchServiceVo
     * @return
     */

    @Override
    public ResPage<List<ServiceInvoiceVo>> queryServiceInvoice(SearchServiceVo searchServiceVo) {
        PageHelper.startPage(searchServiceVo.getPageNum(), searchServiceVo.getPageSize());
        List<ServiceInvoiceVo> serviceInvoices = serviceInvoiceMapper.queryServiceInvoice(searchServiceVo);
        PageInfo info = new PageInfo(serviceInvoices);
        return new ResPage<>(info.getPageNum(),info.getPageSize(), info.getTotal(), info.getPages(), info.getList());
    }

    /**
     * 分配服务单据，修改对应单据的状态
     * @param id
     */
    @Override
    public void updateAssignStatus(String id,Integer assigned) {
        serviceInvoiceMapper.updateAssignStatus(id, Status.SERVICE_STATUS_ASSIGN,assigned);
    }

    /**
     * 服务处理界面根据客户经理id查询状态为已分配的服务单据
     * @param searchServiceVo
     * @return
     */
    @Override
    public ResPage<List<ServiceInvoiceVo>> queryServiceInvoiceById(SearchServiceVo searchServiceVo) {
        PageHelper.startPage(searchServiceVo.getPageNum(), searchServiceVo.getPageSize());
        List<ServiceInvoiceVo> serviceInvoices = serviceInvoiceMapper.queryServiceInvoiceById(searchServiceVo);
        PageInfo info = new PageInfo(serviceInvoices);
        return new ResPage<>(info.getPageNum(),info.getPageSize(), info.getTotal(), info.getPages(), info.getList());
    }

    /**
     * 修改客户经理处理的服务单据状态为已处理，填充处理时间字段
     * @param id
     */
    @Override
    public void updateHandleStatus(String id,String handleWay) {
        serviceInvoiceMapper.updateHandleStatus(id,handleWay,Status.SERVICE_STATUS_HANDLE);
    }

    /**
     * 服务反馈界面根据客户经理id查询状态为已处理的服务单据
     * @param searchServiceVo
     * @return
     */
    @Override
    public ResPage<List<ServiceInvoiceVo>> queryServiceInvoiceByIdAndStatus(SearchServiceVo searchServiceVo) {
        PageHelper.startPage(searchServiceVo.getPageNum(), searchServiceVo.getPageSize());
        List<ServiceInvoiceVo> serviceInvoices = serviceInvoiceMapper.queryServiceInvoiceByIdAndStatus(searchServiceVo);
        PageInfo info = new PageInfo(serviceInvoices);
        return new ResPage<>(info.getPageNum(),info.getPageSize(), info.getTotal(), info.getPages(), info.getList());
    }

    /**
     *
     *客户满意度大于等于3，服务单据状态改为“已归档”。
     * 服务满意度小于3，服务状态改为“已分配”
     * @param id
     * @param mark
     */
    @Override
    public void updateMarks(String id, Integer mark,String content) {
        if(mark >= 3){
            serviceInvoiceMapper.updateMarks(id, mark,Status.SERVICE_STATUS_FADBACK,content);
        }else{
            serviceInvoiceMapper.updateMarks(id, mark,Status.SERVICE_STATUS_ASSIGN,content);
        }
    }

    /**
     * 服务归档页面，查询全部已反馈的单据
     * @return
     */
//    @Override
//    public ResPage<List<ServiceInvoiceVo>> queryServiceInvoiceByStatus(SearchServiceVo searchServiceVo) {
//        PageHelper.startPage(searchServiceVo.getPageNum(), searchServiceVo.getPageSize());
//        List<ServiceInvoiceVo> serviceInvoices = serviceInvoiceMapper.queryServiceInvoice(searchServiceVo);
//        PageInfo info = new PageInfo(serviceInvoices);
//        return new ResPage<>(info.getPageNum(),info.getPageSize(), info.getTotal(), info.getPages(), info.getList());
//    }

    /**
     * 修改单据状态为已归档
     * @param id
     */
    @Override
    public void updateStatus(String id) {
        serviceInvoiceMapper.updateStatus(id,Status.SERVICE_STATUS_ARCHIVIST);
    }

    @Override
    public ResPage<List<ServiceInvoiceVo>> queryServiceInvoiceByStatus(Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        List<ServiceInvoiceVo> serviceInvoices = serviceInvoiceMapper.queryServiceInvoiceByStatus(Status.SERVICE_STATUS_FADBACK,Status.SERVICE_STATUS_ARCHIVIST);
        PageInfo info = new PageInfo(serviceInvoices);
        return new ResPage<>(info.getPageNum(),info.getPageSize(), info.getTotal(), info.getPages(), info.getList());
    }

    //客服服务分析根据类型统计
    @Override
    public List<ServiceTypeCountVo> ServiceTypeCount(String year) {
        return serviceInvoiceMapper.ServiceTypeCount(year);
    }
}
